from lxml import etree
import requests

url = 'https://movie.douban.com/top250'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36'
}
# 发送请求
response = requests.get(url, headers=headers)

# 请求网页源码
page = response.content.decode()
# 把html网页转为html元素对象
html = etree.HTML(page)
# 提取网页中的电影名称
titles = html.xpath('//span[@class="title"][1]/text()')
print(titles)
# 提取电影评分
scores = html.xpath('//span[@class="rating_num"]/text()')
print(scores)

for title, score in zip(titles, scores):
    print('电影名称：',title,'\t评分:', score)
